home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / win / pascal / ownerd.exe / UOWNERDR.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1992-04-16  |  7.0 KB  |  223 lines

  1.  
  2. {
  3.  
  4. Basic owner-draw list box object
  5.  
  6.  
  7. 16th April 1992
  8.  
  9.  
  10. Rex K. Perkins, CIS 70651,1611
  11.  
  12.  
  13.  
  14. }
  15.  
  16.  
  17.  
  18. Unit UOwnerDraw;
  19.  
  20.  
  21. Interface
  22.  
  23. Uses WinTypes, WinProcs, WObjectB;      {Replace WObjectsB with WObjects if 'B' is not available}
  24.  
  25.  
  26. Type
  27.  
  28.     POwnerDrawListBox=^TOwnerDrawListBox;
  29.     TOwnerDrawListBox=Object(TListBox)
  30.  
  31.       Constructor Init(AParent: PWindowsObject; AnId: Integer;
  32.                                 X,Y,W,H:Integer);
  33.  
  34.       Procedure WMDrawItem(DrawStruct:PDrawItemStruct); Virtual;
  35.         {The parent has received a wm_DrawItem message.
  36.         Pass a non nil LParam to this procedure & let it do the rest}
  37.  
  38.       Procedure ChangeFocus(DrawStruct:PDrawItemStruct); Virtual;
  39.         {Change the focus of the ListBox item}
  40.  
  41.       Procedure ChangeSelection(DrawStruct:PDrawItemStruct); Virtual;
  42.         {The selection of item DrawStruct has changed}
  43.  
  44.       Procedure DrawBox(DrawStruct:PDrawItemStruct); Virtual;
  45.         {Draw the item for the ListBox line DrawStruct^.ItemID in the
  46.         box DrawStruct^.rcItem. The colors are already set}
  47.  
  48.       Procedure DrawItem(DrawStruct:PDrawItemStruct); Virtual;
  49.         {Draw the actual item in DrawStruct. Just draw it, everything else is
  50.         taken care of. Must cope with no item selected (item -1)}
  51.  
  52.     End;
  53.  
  54.  
  55.  
  56.  
  57. Implementation
  58.  
  59. Uses Strings;
  60.  
  61.  
  62.     Constructor TOwnerDrawListBox.Init(AParent: PWindowsObject; AnId: Integer;
  63.                                                 X,Y,W,H:Integer);
  64.  
  65.     Begin   {Create the list box, set the OwnerDraw attribute and stop auto-sorting}
  66.       TListBox.Init(AParent,AnID,X,Y,W,H);
  67.       Attr.Style:=(Attr.Style OR lbs_OwnerDrawFixed) AND NOT lbs_Sort
  68.     End;
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.     Procedure TOwnerDrawListBox.WMDrawItem(DrawStruct:PDrawItemStruct);
  78.  
  79.     {The parent has received a wm_DrawItem message.
  80.      Pass a non nil LParam to this procedure & let it do the rest}
  81.  
  82.  
  83.     Begin
  84.       {Ok, what do we want to do? At this point we can interogate both
  85.        ItemAction and ItemState. ItemAction holds the CHANGE in item
  86.        state whereas ItemState holds the desired state AFTER the message.
  87.  
  88.       If we need to change the selection state, call ChangeSelection only
  89.       as this needs to call DrawBox and, if required, ChangeFocus}
  90.  
  91.       If DrawStruct^.ItemAction AND oda_Select>0 Then
  92.         ChangeSelection(DrawStruct)            {The selection changed, redraw this line}
  93.       Else
  94.         Begin       {No change of selection, just draw or change focus}
  95.           If DrawStruct^.ItemAction AND oda_DrawEntire>0 Then
  96.             ChangeSelection(DrawStruct)      {Draw this line of text in the appropriate colors}
  97.           Else
  98.             If DrawStruct^.ItemAction AND oda_Focus>0 Then
  99.               ChangeFocus(DrawStruct)   {The focus changed. Draw / remove the focus box}
  100.         End
  101.     End;
  102.  
  103.  
  104.  
  105.  
  106.     Procedure TOwnerDrawListBox.ChangeFocus(DrawStruct:PDrawItemStruct);
  107.  
  108.     {Change the focus of the ListBox item DrawStruct^.ItemID}
  109.  
  110.     Var
  111.             LineStyle:Integer;
  112.             FGColor:TColorRef;
  113.             NewPen,OldPen:HPen;
  114.             OldBrush,NewBrush:HBrush;
  115.  
  116.     Begin
  117.       If DrawStruct^.ItemState AND ods_Focus>0 Then {This line is getting the focus}
  118.         LineStyle:=ps_Dot
  119.       Else {This line is losing the focus}
  120.         LineStyle:=ps_Solid;
  121.  
  122.       {We now have the line style. Next we need the color. This is determined
  123.       by the line style AND the selected / not selected state}
  124.  
  125.       If (DrawStruct^.ItemID AND $8000 =0) AND (DrawStruct^.ItemState AND ods_Selected>0) Then {This line is selected}
  126.         FGColor:=GetSysColor(Color_Highlight) {Get the standard selected backgtound color}
  127.       Else {This line is de-selected}
  128.         If LineStyle=ps_Dot Then {We are getting the focus, so select the text color}
  129.           FGColor:=GetSysColor(Color_WindowText)
  130.         Else   {We are losing the focus, draw in the backgound color}
  131.           FGColor:=GetSysColor(Color_Window);
  132.  
  133.       NewPen:=CreatePen(LineStyle,1,FGColor);            {Create the pen...}
  134.       OldPen:=SelectObject(DrawStruct^.HDc,NewPen);      {...and select it}
  135.       NewBrush:=GetStockObject(Hollow_Brush);            {Get a hollow brush...}
  136.       OldBrush:=SelectObject(DrawStruct^.HDc,NewBrush);  {...and select it}
  137.  
  138.       Rectangle(DrawStruct^.HDc,DrawStruct^.rcItem.Left,   {Now draw the rectangle}
  139.                                 DrawStruct^.rcItem.Top,
  140.                                 DrawStruct^.rcItem.Right,
  141.                                 DrawStruct^.rcItem.Bottom);
  142.  
  143.       {Finaly, restore the DC's object and delete the pen}
  144.  
  145.       SelectObject(DrawStruct^.HDc,OldPen);
  146.       SelectObject(DrawStruct^.HDc,OldBrush);
  147.  
  148.       DeleteObject(NewPen)
  149.     End;
  150.  
  151.  
  152.  
  153.     Procedure TOwnerDrawListBox.ChangeSelection(DrawStruct:PDrawItemStruct);
  154.  
  155.     {The selection of item DrawStruct has changed}
  156.  
  157.     Var FGColor,BKColor:TColorRef;
  158.         OldFGColor,OldBKColor:TColorRef;
  159.  
  160.     Begin
  161.          {First, get the colors to use}
  162.  
  163.       If (DrawStruct^.ItemState AND ods_Selected>0) Then {This line being selected}
  164.         Begin
  165.           FGColor:=GetSysColor(Color_HighlightText); {Get the standard selected text colors}
  166.           BKColor:=GetSysColor(Color_Highlight)
  167.         End
  168.       Else {This line is being de-selected}
  169.         Begin
  170.           FGColor:=GetSysColor(Color_WindowText); {Get the standard non-selected text colors}
  171.           BKColor:=GetSysColor(Color_Window)
  172.         End;
  173.  
  174.         {Now set the colors for the text}
  175.  
  176.       OldFGColor:=SetTextColor(DrawStruct^.HDc,FGColor);
  177.       OldBKColor:=SetBKColor(DrawStruct^.HDc,BKColor);
  178.  
  179.       DrawBox(DrawStruct);         {Draw the text for this line}
  180.  
  181.       SetTextColor(DrawStruct^.HDc,OldFGColor); {Restore the DC's colors}
  182.       SetBKColor(DrawStruct^.HDc,OldBKColor)
  183.     End;
  184.  
  185.  
  186.  
  187.     Procedure TOwnerDrawListBox.DrawBox(DrawStruct:PDrawItemStruct);
  188.  
  189.     {Draw the item for the ListBox line DrawStruct^.ItemID in the
  190.     box DrawStruct^.rcItem. The colors are already set}
  191.  
  192.     Begin
  193.       Self.DrawItem(DrawStruct);
  194.       If DrawStruct^.ItemState AND ods_Focus>0 Then
  195.         ChangeFocus(DrawStruct)      {This line has the focus, so draw the focus box}
  196.      End;
  197.  
  198.  
  199.  
  200.      Procedure TOwnerDrawListBox.DrawItem(DrawStruct:PDrawItemStruct);
  201.  
  202.      {Draw the actual item in DrawStruct. Just draw it, everything else is
  203.      taken care of. Must cope with no item selected (item Integer(-1))}
  204.  
  205.      Begin
  206.       {If the ItemData points to a string, draw it. Else, draw an empty string}
  207.  
  208.       If (DrawStruct^.ItemID AND $8000 =0) AND (POINTER(DrawStruct^.ItemData)<>Nil) Then
  209.         ExtTextOut(DrawStruct^.HDc, DrawStruct^.rcItem.Left, DrawStruct^.rcItem.Top,
  210.             eto_Clipped OR eto_Opaque,@DrawStruct^.rcItem,PChar(DrawStruct^.ItemData),
  211.             StrLen(PChar(DrawStruct^.ItemData)),Nil)
  212.       Else
  213.         ExtTextOut(DrawStruct^.HDc, DrawStruct^.rcItem.Left, DrawStruct^.rcItem.Top,
  214.             eto_Clipped OR eto_Opaque,@DrawStruct^.rcItem,' ',1,Nil);
  215.  
  216.      End;
  217.  
  218.  
  219.  
  220. Begin
  221. End.
  222.  
  223.